home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume6 / dissect2 < prev    next >
Encoding:
Text File  |  1989-03-04  |  8.5 KB  |  279 lines

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v06i045: Dissection utility for over-large mbox files
  4. Reply-To: mirk@warwick.UUCP (Mike Taylor)
  5. Organization: Computer Science, Warwick University, UK
  6.  
  7. Posting-number: Volume 6, Issue 45
  8. Submitted-by: mirk@warwick.UUCP (Mike Taylor)
  9. Archive-name: dissect2
  10.  
  11. [Okay, so csplit can't be this tricky.  Still, you could do wonders with it
  12. and a shell script wrapper....  Of course, BSD may not have "csplit".  ++bsa]
  13.  
  14. Here is a simple and self-explainatory little number for comp.sources.misc
  15. It should run on any UNIX machine, though I've only tried it on a sun3 with
  16. Berkeley 4.3.  It splits a large mbox into individually named personal
  17. mboxes for each person who has composed one or more of the mbox's
  18. constituent articles.  See the manual page for more details.
  19.  
  20. -------------------------- Cut here, cheese-heads! --------------------------
  21. #! /bin/sh
  22. # This is a shell archive, meaning:
  23. # 1. Remove everything above the #! /bin/sh line.
  24. # 2. Save the resulting text in a file.
  25. # 3. Execute the file with /bin/sh (not csh) to create the files:
  26. #    Makefile
  27. #    Manifest
  28. #    README
  29. #    dissect.1
  30. #    dissect.c
  31. # This archive created: Sat Jan 21 18:30:57 1989
  32. # By:    Mike Taylor ()
  33. export PATH; PATH=/bin:$PATH
  34. if test -f 'Makefile'
  35. then
  36.     echo shar: will not over-write existing file "'Makefile'"
  37. else
  38. cat << \SHAR_EOF > 'Makefile'
  39. all:        dissect.c
  40.         cc -O -s dissect.c -o dissect
  41.         rm -f count
  42.         ln dissect count
  43.  
  44. dissect:    dissect.c
  45.         cc -O -s dissect.c -o dissect
  46.  
  47. count:        dissect.c
  48.         cc -O -s dissect.c -o count
  49. SHAR_EOF
  50. fi # end of overwriting check
  51. if test -f 'Manifest'
  52. then
  53.     echo shar: will not over-write existing file "'Manifest'"
  54. else
  55. cat << \SHAR_EOF > 'Manifest'
  56. -rw-r--r--  1 mirk     csother       182 Jan 21 18:01 Makefile
  57. -rw-r--r--  1 mirk     csother       315 Jan 21 18:28 Manifest
  58. -rw-r--r--  1 mirk     csother       564 Jan 21 18:24 README
  59. -rw-r--r--  1 mirk     csother      1955 Jan 21 18:21 dissect.1
  60. -rw-r--r--  1 mirk     csother      3194 Jan 21 17:57 dissect.c
  61. SHAR_EOF
  62. fi # end of overwriting check
  63. if test -f 'README'
  64. then
  65.     echo shar: will not over-write existing file "'README'"
  66. else
  67. cat << \SHAR_EOF > 'README'
  68. Evening, all.
  69.  
  70. This is a program written in a hurry by me one night because I was
  71. sick of wading through 1/4M mailboxes, trying to find some archaic
  72. piece of correspondance.  It breaks up a large mailbox (or several
  73. of them, if you like) into smaller ones, named after the sender of
  74. the pieces of mail they contain.  See the manual entry if this is
  75. unclear.  Mail bugs, flames, pieces of frozen vomit, slices of
  76. intestinal lining etc., to mirk@uk.ac.warwick.cs.  That's about it
  77. really.  Lap it up!
  78.  
  79. PS.  1st man: "My dog's got not nose"
  80.      2nd man: "Frog off."
  81. SHAR_EOF
  82. fi # end of overwriting check
  83. if test -f 'dissect.1'
  84. then
  85.     echo shar: will not over-write existing file "'dissect.1'"
  86. else
  87. cat << \SHAR_EOF > 'dissect.1'
  88. .\" @(#)dissect.1 1.17 89/01/20 SMI; from HACKERS 1.1
  89. .TH DISSECT 1 "20 January 1989"
  90. .SH NAME
  91. dissect \- Break up an mbox into smaller mboxes
  92. .br
  93. count \- Count number of articles in an mbox
  94. .SH SYNOPSIS
  95. .B dissect
  96. .I filename1
  97. .I [ filename2 ... ]
  98. .br
  99. .B count
  100. .I filename1
  101. .I [ filename2 ... ]
  102. .br
  103. .SH DESCRIPTION
  104. .B dissect
  105. reads through one or more files in mbox format (eg. the file mbox created
  106. by most "mail" programs, and the newsgroup files created by rn(1)).  It
  107. creates new files, each named after the sender of an item of mail in one
  108. of the specified mboxes, and in that file, deposits copies of all mail
  109. sent by that user, so that together, the new files contain exactly the
  110. same data as the old ones.  If the files that would be created already
  111. exist, then
  112. .B dissect
  113. will append the news items in the specified mboxes onto the end of the
  114. existing files.
  115. .B dissect
  116. will refuse to overwrite any of its arguments.
  117. .sp
  118. .B count
  119. counts how many articles are in each mbox specified on the command-line,
  120. and prints this on standard output.
  121. .SH EXAMPLES
  122. example% ls
  123. .br
  124. mbox
  125. .br
  126. example% dissect mbox
  127. .br
  128. example% ls
  129. .br
  130. VIRUS-L     cee074      erict       jec1        mbox
  131. .br
  132. andy        chip.uucp   hjt         martin      weemba
  133. .br
  134. example% count mbox martin hjt
  135. .br
  136. count:  11 items of mail in input file mbox.
  137. .br
  138. count:   1 items of mail in input file martin.
  139. .br
  140. count:   1 items of mail in input file hjt.
  141. .br
  142. example% dissect hjt
  143. .br
  144. dissect: won't overwrite input file hjt.
  145. .SH "SEE ALSO"
  146. .BR mail(1),
  147. .BR rn(1),
  148. .SH BUGS
  149. .B dissect
  150. creates the new files using only the local name of the user who sent
  151. the mail item being saved - thus a piece of mail sent by a user
  152. .B mirk@uk.ac.warwick.cs
  153. would be saved in a file called simply
  154. .B mirk.
  155. .SH AUTHOR
  156. .B dissect
  157. and
  158. .B count
  159. were written by Michael Taylor (mirk@uk.ac.warwick.cs) in the early hours
  160. of the morning of Friday, 20th January, 1989, on Warwick University's
  161. Sun3 "emerald".
  162. SHAR_EOF
  163. fi # end of overwriting check
  164. if test -f 'dissect.c'
  165. then
  166.     echo shar: will not over-write existing file "'dissect.c'"
  167. else
  168. cat << \SHAR_EOF > 'dissect.c'
  169. /****************************************************************************\
  170. |*                                                                          *|
  171. |*  Dissect.c: a rough-and-ready heap of junk to split a file in mbox       *|
  172. |*             format into a number of mbox-format files, each containing   *|
  173. |*             all the messages from a sender whose mail was in the         *|
  174. |*             original mbox, and named after that sender.                  *|
  175. |*                                                                          *|
  176. |*  Also:      it will count the number of articles in each mbox in its     *|
  177. |*             argument list, when called with argv[0] not equal to         *|
  178. |*             dissect.                                                     *|
  179. |*                                                                          *|
  180. |*  This program written in the early hours of 21st January 1989.           *|
  181. |*  Copyright (C) 1989 by Mike Taylor.  No rights reserved - copy me!       *|
  182. |*                                                                          *|
  183. \****************************************************************************/
  184.  
  185. #include <stdio.h>
  186. #include <strings.h>
  187.  
  188. #define LINELEN 1024
  189.  
  190. extern char *fgets ();
  191. static int onlycount = 0;
  192.  
  193. /*--------------------------------------------------------------------------*/
  194.  
  195. int handle (argv, index)
  196.   char **argv;
  197.   int index;
  198. {
  199.   FILE *fp;
  200.   FILE *to = NULL;
  201.   static char name[LINELEN];
  202.   static char line[LINELEN];
  203.   static char last[LINELEN] = "\n";
  204.   char *cp;
  205.   int flag = 0;
  206.  
  207.   if ((fp = fopen (argv[index], "r")) == NULL) {
  208.     (void) fprintf (stderr, "%s: couldn't open input file %s.\n",
  209.             argv[0], argv[index]);
  210.     return (1);
  211.   }
  212.  
  213.   while (fgets (line, LINELEN, fp) != NULL) {
  214.     if ((!strncmp (line, "From ", 5)) && (*last == '\n')) {
  215.       flag++;
  216.       if (!onlycount) {
  217.     (void) fclose (to);
  218.     (void) strcpy (name, line+5);
  219.     for (cp = name; (*cp != ' ') && (*cp != '@') && (*cp != '%'); cp++);
  220.     *cp = '\0';
  221.     if (!strcmp (name, argv[index])) {
  222.       (void) fprintf (stderr, "%s: won't overwrite input file %s.\n",
  223.               argv[0], argv[index]);
  224.       continue;
  225.     }
  226.     if ((to = fopen (name, "a")) == NULL) {
  227.       (void) fprintf (stderr, "%s: couldn't open output file %s.\n",
  228.               argv[0], name);
  229.       return (1);
  230.     }
  231.       }
  232.     }
  233.     if ((to != NULL) && (!onlycount))
  234.       (void) fputs (line, to);
  235.     (void) strcpy (last, line);
  236.   }
  237.   if (flag == 0)
  238.     (void) fprintf (stderr, "%s: found no mail in input file %s.\n",
  239.             argv[0], argv[index]);
  240.   else
  241.     if (onlycount)
  242.       (void) printf ("%s: %3d items of mail in input file %s.\n",
  243.              argv[0], flag, argv[index]);
  244.   return (flag == 0);
  245. }
  246.  
  247. /*--------------------------------------------------------------------------*/
  248.  
  249. main (argc, argv)
  250.   int argc;
  251.   char **argv;
  252. {
  253.   int status = 0;
  254.   int i;
  255.  
  256.   if (argc == 1) {
  257.     (void) fprintf (stderr, "Usage: %s file [ file ... ]\n", argv[0]);
  258.     exit (255);
  259.   }
  260.  
  261.   if (strcmp (argv[0], "dissect"))
  262.     onlycount = 1;
  263.  
  264.   for (i = 1; i < argc; i++)
  265.     status += handle (argv, i);
  266.  
  267.   exit (status);
  268. }
  269.  
  270. /*--------------------------------------------------------------------------*/
  271. SHAR_EOF
  272. fi # end of overwriting check
  273. #    End of shell archive
  274. exit 0
  275. ______________________________________________________________________________
  276. Mike Taylor - {Christ,M{athemat,us}ic}ian ...  Email to: mirk@uk.ac.warwick.cs
  277. *** Unkle Mirk sez: "Em9 A7 Em9 A7 Em9 A7 Em9 A7 Cmaj7 Bm7 Am7 G Gdim7 Am" ***
  278. ------------------------------------------------------------------------------
  279.